package com.dlc.felear.xiaoensale.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dlc.felear.xiaoensale.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class IoItemAdapter extends BaseAdapter {

    private static final String TAG = "IoItemAdapter";
    // 上下文
    private Context context;
    // 数据
//    private List<Integer> items;
    private List<Integer> data;
    private String pre_name="X";

    public IoItemAdapter(Context context, int size) {
        this.context = context;
        data = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            data.add(i);
        }

    }
    public void setPreName(String name){
        pre_name=name;
    }

    public List<Integer> getData() {
        return null;
    }


    public void setItems(Map<Integer, Integer> data) {
//        Log.e(TAG, JsonHelper.toJson(data));
        if (data.isEmpty()) {
            return;
        }
        for(Integer index:data.keySet()){
            if(index<=data.size()){
               this.data.remove(index-1);
               this.data.add(index-1,data.get(index));
            }
        }
//        this.data.clear();
//        this.data.addAll(new ArrayList<Integer>(data.values()));
        notifyDataSetChanged();
    }

    public void clearData() {
//        items.clear();
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Integer getItem(int position) {
        return 0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.adapter_io_item, null);
            holder.tv_name = convertView.findViewById(R.id.tv_name);
            holder.tv_value = convertView.findViewById(R.id.tv_value);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_name.setText(pre_name+ Integer.toString(position+1));
        int value = data.get(position);
        if (value == 1) {
            holder.tv_value.setBackgroundResource(R.drawable.io_statu_1_shape);
        } else {
            holder.tv_value.setBackgroundResource(R.drawable.io_statu_2_shape);
        }
        return convertView;
    }

    static class ViewHolder {
        TextView tv_name, tv_value;
    }

}